package com.wxc.api.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxc.api.admin.entity.SysUser;
import com.wxc.api.admin.mapper.SysUserMapper;
import com.wxc.api.admin.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxc.common.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-04-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public SysUser login(String account, String password) {
        QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
        userWrapper.select("id", "account", "user_name", "enabled", "account_not_expired", "account_not_locked", "password");
        userWrapper.eq("account", account);
        // userWrapper.eq("password", password);
        return sysUserMapper.selectOne(userWrapper);
    }

    @Override
    public Result login2(String account, String password) {

        QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
        userWrapper.select("id", "account", "user_name", "enabled", "account_not_expired", "account_not_locked");
        userWrapper.eq("account", account);
        // userWrapper.eq("password", password);
        return Result.success(StpUtil.getTokenInfo());
    }

    @Override
    public Result logout(HttpServletRequest request) {
        return null;
    }

    @Override
    public Integer add(String account,String userName,String password) {
        QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
        userWrapper.eq("account", account);
        SysUser userInfo = sysUserMapper.selectOne(userWrapper);
        if(userInfo != null){
            return -1;
        }

        String newPassword = bCryptPasswordEncoder.encode(password);

        SysUser sysUser = new SysUser();
        sysUser.setAccount(account);
        sysUser.setUserName(userName);
        sysUser.setPassword(newPassword);
        sysUser.setCreateUser(StpUtil.getLoginIdAsInt());
        sysUser.setUpdateUser(StpUtil.getLoginIdAsInt());
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public SysUser info() {
        QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
        userWrapper.select("id", "account", "user_name", "enabled", "account_not_expired", "account_not_locked", "last_login_time");
        userWrapper.eq("id", StpUtil.getLoginIdAsInt());
        return sysUserMapper.selectOne(userWrapper);
    }

    @Override
    public Integer del(Integer id) {
        QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
        userWrapper.eq("id", id);
        return sysUserMapper.delete(userWrapper);
    }

    @Override
    public Integer enabled(Integer id, boolean enabled) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setEnabled(enabled);
        sysUser.setUpdateUser(StpUtil.getLoginIdAsInt());

        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public void setLoginTime() {
        LocalDateTime dateTime = LocalDateTime.now();

        SysUser sysUser = new SysUser();
        sysUser.setId(StpUtil.getLoginIdAsInt());
        sysUser.setLastLoginTime(dateTime);

        sysUserMapper.updateById(sysUser);
    }

    @Override
    public Integer setPassword(String password) {
        String newPassword = bCryptPasswordEncoder.encode(password);
        int loginId = StpUtil.getLoginIdAsInt();

        SysUser sysUser = new SysUser();
        sysUser.setId(loginId);
        sysUser.setPassword(newPassword);
        sysUser.setUpdateUser(StpUtil.getLoginIdAsInt());

        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public Integer setUserPassword(Integer id, String password) {
        String newPassword = bCryptPasswordEncoder.encode(password);

        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setPassword(newPassword);
        sysUser.setUpdateUser(StpUtil.getLoginIdAsInt());

        return sysUserMapper.updateById(sysUser);
    }



}
